package com.system.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.system.dao.StudentDao;
import com.system.model.Student;
import com.system.util.JdbcUtil;
import com.system.util.ResultMapUtil;

public class StudentDaoImpl extends BaseDaoImpl implements StudentDao {

	public StudentDaoImpl() throws Exception {
		super();
	}
	
	/**
	 * 对查找的重复操作进行封装
	 * @return
	 */
	private ArrayList<Student> find(String sql){
		try{
			ResultSet rs=this.templateQuery(sql);
			boolean find = false;
			ArrayList<Student> students = new ArrayList<>();
			while(rs.next()) {
				find = true;
				Student stu =new Student();
				Map<String,String> map=ResultMapUtil.getResultMap(rs);
				try{
					BeanUtils.populate(stu, map);
					students.add(stu);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(!find) {
				return null;
			}
			return students;
				
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public ArrayList<Student> findAll() {
		String sql="select * from "+"student";
		ArrayList<Student> stus=find(sql);
		JdbcUtil.getInstance().free(conn);
		return stus;
	}
	
	@Override
	public List<Student> findbyClass(String clazz) {
		String sql="select * from "+"student"+" where stuclass="+"'"+clazz+"'";
		ArrayList<Student> stus=find(sql);
		JdbcUtil.getInstance().free(conn);
		return stus;
	}
	
	@Override
	public Student findById(int user_id) {
		String sql="select * from "+"student"+" where user_id="+user_id;
		ArrayList<Student> stus=find(sql);
		//如果查询结果为null
		if(stus == null) {
			return null;
		}
		Student stu = stus.get(0);
		JdbcUtil.getInstance().free(conn);
		return stu;
	}
	
	@Override
	public String findClass(String stunum){
		String sql="select stuclass from student where stunum="+"'"+stunum+"'";
		try{
			String stuclass=null;
			ResultSet rs=this.templateQuery(sql);
			if(rs.next()){
				stuclass=rs.getString(1);
			}
			return stuclass;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
	}
	
	@Override
	public Student querybystunum(String stunum){
		String sql="select * from "+"student"+" where stunum="+stunum;
		ArrayList<Student> stus=find(sql);
		//如果查询结果为null
		if(stus == null) {
			return null;
		}
		Student stu = stus.get(0);
		JdbcUtil.getInstance().free(conn);
		return stu;
		
	}

	@Override
	public boolean add(Student stu) {
		String sql="insert into "+"student(stuname,stuclass"+
				",major,user_id,stunum)"+ "values"+"("+
				"'"+stu.getStuname()+"'"+
				","+"'"+stu.getStuclass()+"'"+
				","+"'"+stu.getMajor()+"'"+
				","+stu.getUser_id()+","
				+"'"+stu.getStunum()+"'"+")";
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		
	}

	@Override
	public boolean deleteStudents(int[] stuIds) {
		try{
			for(int id:stuIds){
				String sql="delete from "+"student"+" where user_id="
						+id;
				this.templateUpdate(sql);
				sql="delete from "+"user"+" where id="
						+id;
				this.templateUpdate(sql);
			}
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		return false;
	}

	@Override
	public boolean modify(Student stu) {
		String sql="update student set stuname="+"'"+stu.getStuname()+"'"+","
				+ " stuclass="+"'"+stu.getStuclass()+"'"+","
				+" major="+"'"+stu.getMajor()+"'"+","
				+" user_id="+stu.getUser_id()+","
				+" stunum="+"'"+stu.getStunum()+"'"
				+" where user_id="+stu.getUser_id();
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		return false;
	}

	@Override
	public Integer findId(String stunum) {
		String sql="select id from student where stunum= "+"'"+stunum+"'";
		try {
			ResultSet rs=this.templateQuery(sql);
			int res = -1;
			if(rs.next()){
				res=(Integer)rs.getObject("id");
			}
			return res;
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}


}
